import 'package:flutter/material.dart';

class GridViewApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new GridIViewState();
  }
}

class GridIViewState extends State<GridViewApp> {
  List<GridItem> grids = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    for (int i = 0; i < 100; i++) {
      grids.add(
          new GridItem("这个测试数据$i", i % 2 > 0 ? Icons.cake : Icons.ac_unit));
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("GridView"),
        ),
        body: new GridView.builder(
          gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 10.0,
              crossAxisSpacing: 10.0,
              childAspectRatio: 1.0),
          itemBuilder: (BuildContext context, int index) {
            return new GridViewItemWidget(grids[index]);
          },
          itemCount: grids.length,
        ));
  }
}

class GridViewItemWidget extends StatelessWidget {
  GridItem gridItem;

  GridViewItemWidget(this.gridItem);

  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
      child: new Container(
        color: Colors.amberAccent,
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Icon(
              gridItem.iconData,
              size: 50.0,
            ),
            new Text(gridItem.title)
          ],
        ),
      ),
      onTap: () {
        Scaffold.of(context).showSnackBar(
            new SnackBar(content: new Text(gridItem.title)));
      },
    );
  }
}

class GridItem {
  String title;

  IconData iconData;

  GridItem(this.title, this.iconData);
}
